package com.gallsoft.amidar;

import com.gallsoft.amidar.HotSpotSystem.HotSpotType;

import android.graphics.Point;

public class GameWorld extends AllocationGuard {
    private Point[] junctions; 
	private int[] directions;
	private int maxsize, pointer;

	public GameWorld(int size) {
		junctions = new Point[size];
		directions = new int[size];
		pointer = 0;
		maxsize = size;
		
		for (int i = 0; i < maxsize; i++) {
			junctions[i] = new Point();
		}
	}
	
	/**
	 * Add a junction to the game world
	 * @param x x coordinate
	 * @param y y coordinate
	 * @param validDir valid directions
	 * @return Number of junctions or -1 if world is full
	 */
	public int addJunction(int x, int y, int validDir) {
		if (pointer < maxsize) {
			junctions[pointer].set(x, y);
			directions[pointer++] = validDir;
			
			return pointer;
		}
		else
			return -1;
	}
	
	public int getJunctionID(float x, float y, int newDir) {
		int i = 0;
		int xDiff, yDiff;
		boolean isJunction = false;
		int junctionID = -1;
		
		while (!isJunction && i < pointer) {
			xDiff = (int)Math.abs(x - junctions[i].x);
			yDiff = (int)Math.abs(y - junctions[i].y);
			
			if (xDiff < 4 && yDiff < 4) {
				isJunction = true;
				junctionID = i;
			}

			i++;
		}
		
		//return availDir;
		return junctionID;
	}
	
	public int getDirections(int index) {
		return directions[index];
	}
	
	public Point getJunction(int index) {
		return junctions[index];
	}
}
